package com.bishe.bishe.dao.mapper;

import com.bishe.bishe.pojo.Advertisements;
import com.bishe.bishe.pojo.Orders;
import org.apache.ibatis.annotations.*;
import tk.mybatis.mapper.common.Mapper;

import java.util.List;

public interface OrderMapper<Repository> extends Mapper<Orders> {
    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders o,state s,customer c where s.state_name = o.order_state and o.order_consumer_id=c.customer_id")
    List<Orders> findall() ;

    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders o,state s,customer c  where order_id=#{orderId} and order_state=#{stateName} and s.state_name = o.order_state and o.order_consumer_id=c.customer_id")
    List<Orders> finddetailByRandom(@Param("orderId") Object orderId, @Param("stateName") String stateName) ;

    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders  where order_id=#{orderId}")
    List<Orders> finddetailById(Integer orderId) ;

    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders  where order_state=#{stateName}")
    List<Orders> findorderByName(@Param("stateName") String stateName) ;



    @Update("update orders set order_total=#{orders.orderTotal} where order_id=#{orders.orderId}")
    int update(@Param("orders") Orders orders);
    @Update("update advertisement set adver_content=#{advertisement} where adver_id=1")
    int updateAdvertisement(@Param("advertisement") String  advertisement);

    @Select("select * from advertisement where adver_id=1")
    List<Advertisements> selectAdvert();

    @Update("update orders set order_state=#{orders.state.stateName} where order_id=#{orders.orderId}")
    int updateState(@Param("orders") Orders orders);
    @Update("update orders set order_state=#{orders.state.stateName},order_date=#{orders.orderDate} where order_id=#{orders.orderId}")
    int updateStateDate(@Param("orders") Orders orders);

    @Update("update orders set order_state=#{orders.state.stateName}，order_reason=#{orders.orderReason} where order_id=#{orders.orderId}")
    int updateReason(@Param("orders") Orders orders);

    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders o,state s,customer c where s.state_name = o.order_state and o.order_consumer_id=c.customer_id and c.customer_number=#{customerNumber} and o.order_identify=#{orderIdentify}")
    List<Orders> findByNumber(@Param("customerNumber") String customerNumber,@Param("orderIdentify") int orderIdentify) ;

    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders o,state s,customer c where s.state_name = o.order_state and o.order_consumer_id=c.customer_id and c.customer_number=#{customerNumber} ORDER BY o.order_time DESC")
    List<Orders> findByWx(@Param("customerNumber") String customerNumber) ;

    @Insert("insert into orders(order_id,order_time,order_state,order_total,order_consumer_id,order_identify,order_taketime,order_express,order_remarks) value(null,#{orderTime},'未确认',#{orderTotal},#{customer.customerId},#{orderIdentify},#{orderTaketime},#{express.expressName},#{orderRemarks})")
    @Options(useGeneratedKeys=true, keyProperty="orderId", keyColumn="order_id")
    int insert( Orders orders);

    @Insert("insert into orders(order_id,order_time,order_state,order_total,order_consumer_id,order_identify,order_express,order_remarks) value(null,#{orderTime},'未确认',#{orderTotal},#{customer.customerId},#{orderIdentify},#{express.expressName},#{orderRemarks})")
    @Options(useGeneratedKeys=true, keyProperty="orderId", keyColumn="order_id")
    int indexinsert( Orders orders);


    @Select("select * from orders  where order_express='要' and order_state='已确认'")
    List<Orders> findexpress();


    @Results({
            @Result(property = "orderId",column = "order_id"),
            @Result(property = "orderTime",column = "order_time"),
            @Result(property = "state",column = "order_state",one = @One(select = "com.bishe.bishe.dao.mapper.StateMapper.findall")),
            @Result(property = "orderTotal",column = "order_total"),
            @Result(property = "customer",column = "order_consumer_id",one = @One(select = "com.bishe.bishe.dao.mapper.CustomerMapper.findall")),
            @Result(property = "orderIdentify",column = "order_identify"),
            @Result(property = "orderTaketime",column = "order_taketime"),
            @Result(property = "express",column = "order_express",one = @One(select = "com.bishe.bishe.dao.mapper.ExpressMapper.findall")),
            @Result(property = "orderReason",column = "order_reason"),
            @Result(property = "orderRemarks",column = "order_remarks")
    })
    @Select("select * from orders  where order_id=#{orderId} and order_identify=#{checkId}")
    Orders findByIdcheckId(@Param("orderId") Long orderId,@Param("checkId")int checkId);


    @Select("SELECT * FROM orders WHERE TO_DAYS( NOW( ) ) - TO_DAYS( order_date) <= 1")
    List<Orders> count();
}
